package com.neegix.organization.post.infrastructure.repository.impl;

import com.neegix.application.query.NebulaSQL;
import com.neegix.base.PageVO;
import com.neegix.organization.post.application.assembler.PostAssembler;
import com.neegix.organization.post.application.cqrs.query.PostQueryRepository;
import com.neegix.organization.post.application.cqrs.query.condition.PostWhereGroup;
import com.neegix.organization.post.application.dto.PostDTO;
import com.neegix.organization.post.interfaces.vo.PostVO;
import com.neegix.organization.post.infrastructure.repository.convert.PostConverter;
import com.neegix.organization.post.infrastructure.repository.dataobject.PostDO;
import com.neegix.organization.post.infrastructure.repository.mapper.PostMapper;
import com.neegix.organization.post.infrastructure.repository.mapper.customized.PostCustomizedMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

 /**
  * This file is auto-generated by nebula-framework-generator.
  * The auto-generation plugin was developed using IntelliJ IDEA Community Edition.
  * Thanks to JetBrains for their outstanding contributions to the developer community.
  * <p>
  * The code generated by this tool is owned by the user of the tool.
  * The tool itself is copyrighted by <a href="https://www.neegix.com">https://www.neegix.com</a>.
  *
  * @author <a href="https://www.neegix.com">https://www.neegix.com</a>
  * @version 1.0.0
  * @since 2024-11-25 11:27:14
  */

@Repository
public class PostQueryRepositoryImpl implements PostQueryRepository {

    @Autowired
    private PostMapper postMapper;

    @Autowired
    private PostCustomizedMapper postCustomizedMapper;

    @Override
    public Optional<PostVO> findById(Long id) {
        PostDO postDO = postMapper.selectByPrimaryKey(id);
        return Optional.ofNullable(PostAssembler.INSTANCE.covertVO(PostConverter.INSTANCE.covertDTO(postDO)));
    }

    @Override
    public Integer selectCount(List<Long> ids) {
        return postCustomizedMapper.selectCountByIds(ids);
    }

    @Override
    public PageVO<PostVO> findPage(Integer currentPage, Integer pageSize, PostDTO postDTO) {
        NebulaSQL nebulaSQL = new NebulaSQL();
        nebulaSQL.createWhereGroups(PostWhereGroup.class)
           .andCodeLikeTo(postDTO.getCode())
           .andCreateTimeBetween(postDTO.getStartCreateTime(),postDTO.getEndCreateTime())
           .andCreateUserEqualTo(postDTO.getCreateUser())
           .andDeletedEqualTo(postDTO.getDeleted())
           .andEnabledEqualTo(postDTO.getEnabled())
           .andIdEqualTo(postDTO.getId())
           .andNameLikeTo(postDTO.getName())
           .andPkBizUnitEqualTo(postDTO.getPkBizUnit())
           .andPkDeptEqualTo(postDTO.getPkDept())
           .andUpdateTimeBetween(postDTO.getStartUpdateTime(),postDTO.getEndUpdateTime())
           .andUpdateUserEqualTo(postDTO.getUpdateUser());
           
        nebulaSQL.setPager(currentPage, pageSize);
        List<PostDO> result = postMapper.selectList(nebulaSQL);
        Long total = postMapper.selectCount(nebulaSQL);
        PageVO<PostVO> page = new PageVO<>(currentPage, pageSize);
        page.setTotal(total);
        page.setResult(PostAssembler.INSTANCE.covertVO(PostConverter.INSTANCE.covertDTOS(result)));
        return page;
    }
}
